package com.netease.demo.shortvideo;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import com.netease.demo.shortvideo.config.Preferences;
import com.netease.demo.shortvideo.util.crash.AppCrashHandler;
import com.netease.demo.shortvideo.util.sys.SystemUtil;
import com.netease.nim.uikit.NimUIKit;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.netease.nim.uikit.common.util.storage.StorageType;
import com.netease.nim.uikit.common.util.storage.StorageUtil;
import com.netease.nim.uikit.common.util.sys.ScreenUtil;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.auth.LoginInfo;

/**
 * Created by hzxuwen on 2017/4/5.
 */

public class ShortVideoApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();

        // init tools
        AppCrashHandler.getInstance(this);
        DemoCache.setContext(this);
        NIMClient.init(this, getLoginInfo(), null);

        StorageUtil.init(this, null);
        ScreenUtil.init(this);
        initLog();

        if(inMainProcess()) {

            // 初始化UIKit模块
            NimUIKit.init(this, null, null);
        }
    }

    public boolean inMainProcess() {
        String packageName = getPackageName();
        String processName = SystemUtil.getProcessName(this);
        return packageName.equals(processName);
    }

    private void initLog() {
        String path = StorageUtil.getDirectoryByDirType(StorageType.TYPE_LOG);
        LogUtil.init(path, Log.DEBUG);
        LogUtil.i("demo", " demo log path=" + path);
    }

    private LoginInfo getLoginInfo() {
        String account = Preferences.getUserAccount();
        String token = Preferences.getUserToken();
        String vodToken = Preferences.getVoidToken();

        if (!TextUtils.isEmpty(vodToken)) {
            DemoCache.setVodtoken(vodToken);
        }

        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token)) {
            DemoCache.setAccount(account.toLowerCase());
            return new LoginInfo(account, token);
        } else {
            return null;
        }
    }
}
